#!/usr/bin/env bash
# Copyright (c) Microsoft. All rights reserved.

# Some settings are used to connect to an external dependency, e.g. Azure IoT Hub and IoT Hub Manager API
# Depending on which settings and which dependencies are needed, edit the list of variables checked

# Before checking all the env vars, detect whether secrets, usually encrypted, are available or not.
# Secrets are not available when building a pull request, so the script will not check for those.
detect_secrets() {
    SECRETS_AVAILABLE="true"
    if [[ "$TRAVIS_PULL_REQUEST" != "" && "$TRAVIS_PULL_REQUEST" != "false" ]]; then
        SECRETS_AVAILABLE="false"
        echo "Warning: secrets and encrypted variables are not available when testing pull requests."
    fi
}

detect_secrets

if [[ -z "$PCS_TELEMETRY_DOCUMENTDB_CONNSTRING" && "$SECRETS_AVAILABLE" = "true" ]]; then
    echo "Error: the PCS_TELEMETRY_DOCUMENTDB_CONNSTRING environment variable is not defined."
    exit -1
fi

if [[ -z "$PCS_STORAGEADAPTER_WEBSERVICE_URL" ]]; then
    echo "Error: the PCS_STORAGEADAPTER_WEBSERVICE_URL environment variable is not defined."
    exit -1
fi

if [[ -z "$PCS_AUTH_WEBSERVICE_URL" ]]; then
    echo "Error: the PCS_AUTH_WEBSERVICE_URL environment variable is not defined."
    exit -1
fi

if [[ -z "$PCS_AUTH_ISSUER" ]]; then
    echo "Error: the PCS_AUTH_ISSUER environment variable is not defined."
    exit -1
fi

if [[ -z "$PCS_AUTH_AUDIENCE" ]]; then
    echo "Error: the PCS_AUTH_AUDIENCE environment variable is not defined."
    exit -1
fi

if [[ -z "$PCS_TELEMETRY_STORAGE_TYPE" ]]; then
    echo "Error: the PCS_TELEMETRY_STORAGE_TYPE environment variable is not defined."
    exit -1
fi

# The settings below are for Time Series Insights. If your deployment does not use
# Time Series Insights they are safe to remove.

if [[ -z "$PCS_AAD_TENANT" ]]; then
    echo "Error: the PCS_AAD_TENANT environment variable is not defined."
    exit -1
fi

if [[ -z "$PCS_AAD_APPID" ]]; then
    echo "Error: the PCS_AAD_APPID environment variable is not defined."
    exit -1
fi

if [[ -z "$PCS_AAD_APPSECRET" ]]; then
    echo "Error: the PCS_AAD_APPSECRET environment variable is not defined."
    exit -1
fi

if [[ -z "$PCS_TSI_FQDN" ]]; then
    echo "Error: the PCS_TSI_FQDN environment variable is not defined."
    exit -1
fi

# Settings for actions
if [[ -z "$PCS_ACTION_EVENTHUB_NAME" ]]; then
    echo "Error: the PCS_ACTION_EVENTHUB_NAME environment variable is not defined."
    exit -1
fi

if [[ -z "$PCS_ACTION_EVENTHUB_CONNSTRING" ]]; then
    echo "Error: the PCS_ACTION_EVENTHUB_CONNSTRING environment variable is not defined."
    exit -1
fi

if [[ -z "$PCS_LOGICAPP_ENDPOINT_URL" ]]; then
    echo "Error: the PCS_LOGICAPP_ENDPOINT_URL environment variable is not defined."
    exit -1
fi

if [[ -z "$PCS_AZUREBLOB_CONNSTRING" ]]; then
    echo "Error: the PCS_AZUREBLOB_CONNSTRING environment variable is not defined."
    exit -1
fi

if [[ -z "$PCS_SOLUTION_WEBSITE_URL" ]]; then
    echo "Error: the PCS_SOLUTION_WEBSITE_URL environment variable is not defined."
    exit -1
fi

